<?php


namespace App\Model;


use Illuminate\Database\Eloquent\Model;

class Order extends Model
{
    const CREATED_AT = 'createtime';
    const UPDATED_AT = 'updatetime';

    //表名
    protected $table = 'order';

    // 追加属性
    protected $appends = [
        'status_text',
        'refund_status_text'
    ];

    public function getActualPayAttribute($value)
    {
        return bcdiv($value, 100, 2);
    }

    protected function setActualPayAttribute($value)
    {
        return bcmul($value, 100, 0);
    }

    public function getStatusTextAttribute()
    {
        $value = $this->status;
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getRefundStatusTextAttribute()
    {
        $value = $this->refund_status;
        $list = $this->getRefundStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getStatusList()
    {
        return [1 => '待发货', 2 => '待收货', 3 => '已完成', 4 => '已退款'];
    }

    public function getRefundStatusList()
    {
        return [1 => '未退款', 2 => '部分退', 3 => '全部退'];
    }

    /**
     * 关联订单商品表
     */
    public function orderGoods()
    {
        return $this->hasMany('App\Model\OrderGoods', 'order_id', 'id');
    }
}
